-- ============================================
-- ROULETTE - ComputerCraft / CC:Tweaked
-- Nécessite : Advanced Computer + Advanced Monitor (couleur)
-- ============================================

-- === CONFIGURATION DE LA ROUE (europeenne, 0 a 36) ===
local RED_NUMBERS = {
    [1]=true,[3]=true,[5]=true,[7]=true,[9]=true,[12]=true,[14]=true,[16]=true,
    [18]=true,[19]=true,[21]=true,[23]=true,[25]=true,[27]=true,[30]=true,[32]=true,
    [34]=true,[36]=true,
}

local function colorOf(n)
    if n == 0 then return "VERT", colors.green end
    if RED_NUMBERS[n] then return "ROUGE", colors.red end
    return "NOIR", colors.black
end

local MISE = 1
local points = 100

-- === PERIPHERIQUES ===
local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

mon.setTextScale(1)
local w, h = mon.getSize()

-- === UTILITAIRES D'AFFICHAGE ===
local function centerText(y, text, textColor, bgColor)
    mon.setBackgroundColor(bgColor or colors.black)
    mon.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    mon.setCursorPos(x, y)
    mon.write(text)
end

local function clear()
    mon.setBackgroundColor(colors.black)
    mon.clear()
end

-- Zones des boutons de mise (rouge / noir / vert)
local buttons = {}

local function drawButtons(selected)
    local labels = { "ROUGE", "NOIR", "VERT" }
    local cols = { colors.red, colors.gray, colors.green }
    local btnWidth = math.floor(w / 3)
    buttons = {}

    for i = 1, 3 do
        local x1 = (i - 1) * btnWidth + 1
        local x2 = x1 + btnWidth - 1
        local y = h - 2

        local bg = cols[i]
        if selected == i then
            mon.setBackgroundColor(colors.white)
        else
            mon.setBackgroundColor(bg)
        end
        mon.setTextColor(colors.white)
        for x = x1, x2 do
            mon.setCursorPos(x, y)
            mon.write(" ")
        end
        local labelX = x1 + math.floor((btnWidth - #labels[i]) / 2)
        mon.setCursorPos(labelX, y)
        mon.write(labels[i])

        buttons[i] = { x1 = x1, x2 = x2, y = y, name = labels[i] }
    end
end

local function drawFrame(message, msgColor)
    clear()
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "ROULETTE", colors.white)
    centerText(4, "Points : " .. points, colors.lime)
    if message then
        centerText(6, message, msgColor or colors.white)
    end
    centerText(h - 4, "Choisis ta mise :", colors.lightGray)
    drawButtons(nil)
end

-- Anime le tirage du numero (defile rapidement puis ralentit)
local function spinWheel()
    for step = 1, 15 do
        local n = math.random(0, 36)
        local cname, ccol = colorOf(n)
        clear()
        centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
        centerText(2, "ROULETTE", colors.white)
        centerText(4, "Points : " .. points, colors.lime)
        centerText(math.floor(h/2), tostring(n) .. " - " .. cname, ccol == colors.black and colors.lightGray or ccol)
        sleep(0.06 + step * 0.015)
    end

    local finalNumber = math.random(0, 36)
    local cname, ccol = colorOf(finalNumber)
    return finalNumber, cname, ccol
end

local function payout(betName, resultName, number)
    if betName == resultName then
        if betName == "VERT" then
            return 14 -- 0 est rare (1/37), gain plus fort
        else
            return 2
        end
    end
    return 0
end

local function playRound(betIndex)
    if points < MISE then
        drawFrame("PLUS ASSEZ DE POINTS !", colors.red)
        sleep(2)
        return
    end

    local betName = buttons[betIndex].name
    points = points - MISE

    drawFrame("Mise sur " .. betName .. " ...", colors.yellow)
    sleep(0.5)

    local number, resultName, resultColor = spinWheel()

    local gain = payout(betName, resultName, number)
    points = points + gain

    clear()
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "ROULETTE", colors.white)
    centerText(4, "Points : " .. points, colors.lime)
    centerText(6, "Resultat : " .. number .. " (" .. resultName .. ")",
        resultColor == colors.black and colors.lightGray or resultColor)

    if gain > 0 then
        centerText(8, "GAGNE ! +" .. gain, colors.lime)
    else
        centerText(8, "Perdu... reessaie !", colors.red)
    end

    sleep(2)
end

-- === PROGRAMME PRINCIPAL ===
drawFrame()

while true do
    local event, side, x, y = os.pullEvent("monitor_touch")

    for i, btn in pairs(buttons) do
        if x >= btn.x1 and x <= btn.x2 and y == btn.y then
            playRound(i)
            drawFrame()
            break
        end
    end
end